<template :xs="13" :sm="24" :md="20" :lg="24" >
  <div :xs="13" :sm="24" :md="20" :lg="24" >
    <el-row  :xs="13" :sm="24" :md="20" :lg="24" >
      <el-col >
        <!-- <el-col :span="5"> -->
        <el-card shadow="never" :body-style="contentViewHeight" >
          <el-tag closable size="small" style="margin-bottom:5px" v-if="selectJpEvidenceName" :disable-transitions="false"
            @close="handleNodeClose">
            {{selectJpEvidenceName}}
          </el-tag>
          <el-row :gutter="5">
            <el-col :span="20">
              <el-input placeholder="输入关键字进行过滤" size="small" v-model="filterText">
              </el-input>
            </el-col>
            <el-col :span="4">
              <el-button type="primary" @click="addTreeNode" size="small" icon="el-icon-plus" circle></el-button>
            </el-col>
          </el-row>
          <el-tree class="filter-tree" :data="jpEvidenceTreeData" :props="{
              value: 'id',             // ID字段名
              label: 'name',         // 显示名称
              children: 'children'    // 子级字段名
            }"
            default-expand-all :filter-node-method="filterNode" :expand-on-click-node="false"
            ref="jpEvidenceTree"
            :xs="13" :sm="24" :md="20" :lg="24"
            >
            <span :xs="13" :sm="24" :md="20" :lg="24" class="custom-tree-node" slot-scope="{ node, data}">
             <span @click="jump(node.label)">{{ node.label }}</span>
              <span>
                <el-button type="text" class="tree-item-button" icon="el-icon-plus" @click="() => addChildTreeNode(data)">
                </el-button>
                <el-button type="text" class="tree-item-button" icon="el-icon-edit-outline" @click="() => editTreeNode(data)">
                </el-button>
                <el-button type="text" class="tree-item-button" icon="el-icon-delete" @click="() => delTreeNode(data)">
                </el-button>
              </span>
            </span>
          </el-tree>
        </el-card>
      </el-col>
    </el-row>
    <!-- 弹窗, 新增 / 修改 -->
    <JpContentForm ref="jpContentForm" @refreshDataList="refreshList"></JpContentForm>
    <JpEvidenceForm ref="jpEvidenceForm" @refreshTree="refreshTree"></JpEvidenceForm>

  </div>
</template>

<script>
  import JpContentForm from './JpContentForm'
  import JpEvidenceForm from './JpEvidenceForm'
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  export default {
    data() {
      return {
        searchForm: {
          content: {
            id: ''
          }
        },
        filterText: '',
        jpEvidenceTreeData: [],
        selectJpEvidenceName: '',
        dataList: [],
        pageNo: 1,
        pageSize: 10,
        total: 0,
        orderBy: '',
        dataListSelections: [],
        isSearchCollapse: false,
        isImportCollapse: false,
        loading: false
      }
    },
    components: {
      SelectTree,
      JpContentForm,
      JpEvidenceForm
    },
    activated() {
      this.refreshTree()
      this.refreshList()
    },
    computed: {
      contentViewHeight() {
        let height = this.$store.state.common.documentClientHeight - 122
        return {
          minHeight: height + 'px'
        }
      }
    },
    watch: {
      filterText(val) {
        this.$refs.jpEvidenceTree.filter(val)
      }
    },
    methods: {
      filterNode(value, data) {
        if (!value) return true
        return data.name.indexOf(value) !== -1
      },
      refreshTree() {
        this.$http({
          url: `/guidance/jpEvidence/treeData`,
          method: 'get'
        }).then(({
          data
        }) => {
          this.jpEvidenceTreeData = data.treeData
        })
      },
      handleNodeClick(data) {
        this.searchForm.content.id = data.id
        this.selectJpEvidenceName = '已选: ' + data.name
        this.refreshList()
      },
      handleNodeClose() {
        this.searchForm.content.id = ''
        this.selectJpEvidenceName = ''
        this.refreshList()
      },
      addChildTreeNode(node) {
        this.$refs.jpEvidenceForm.init('addChild', {
          id: '',
          parent: {
            id: node.id,
            name: node.name
          }
        })
      },
      // 新增
      addTreeNode() {
        this.$refs.jpEvidenceForm.init('add', {
          id: '',
          parent: {
            id: '',
            name: ''
          }
        })
      },
      // 修改
      editTreeNode(data) {
        this.$refs.jpEvidenceForm.init('edit', {
          id: data.id,
          parent: {
            id: '',
            name: ''
          }
        })
      },
      delTreeNode(data) {
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/guidance/jpEvidence/delete',
            method: 'delete',
            params: {
              'id': data.id
            }
          }).then(({
            data
          }) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.loading = false
              this.refreshTree()
              this.refreshList()
            }
          })
        })
      },
      // 获取数据列表
      refreshList() {
        this.loading = true
        this.$http({
          url: '/guidance/jpContent/list',
          method: 'get',
          params: {
            'pageNo': this.pageNo,
            'pageSize': this.pageSize,
            'orderBy': this.orderBy,
            ...this.searchForm
          }
        }).then(({
          data
        }) => {
          if (data && data.success) {
            this.dataList = data.page.list
            this.total = data.page.count
            this.loading = false
          }
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageNo = 1
        this.refreshList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageNo = val
        this.refreshList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },

      // 排序
      sortChangeHandle(obj) {
        if (obj.order === 'ascending') {
          this.orderBy = obj.prop + ' asc'
        } else if (obj.order === 'descending') {
          this.orderBy = obj.prop + ' desc'
        } else {
          this.orderBy = ''
        }
        this.refreshList()
      },
      // 新增
      add() {
        this.$refs.jpContentForm.init('add', '')
      },
      // 修改
      edit(id) {
        id = id || this.dataListSelections.map(item => {
          return item.id
        })[0]
        this.$refs.jpContentForm.init('edit', id)
      },
      // 查看
      view(id) {
        this.$refs.jpContentForm.init('view', id)
      },
      // 删除
      del(id) {
        let ids = id || this.dataListSelections.map(item => {
          return item.id
        }).join(',')
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/guidance/jpContent/delete',
            method: 'delete',
            params: {
              'ids': ids
            }
          }).then(({
            data
          }) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.refreshList()
            }
            this.loading = false
          })
        })
      },
      // 导入成功
      uploadSuccess(res, file) {
        if (res.success) {
          this.$message.success({
            dangerouslyUseHTMLString: true,
            message: res.msg
          })
        } else {
          this.$message.error(res.msg)
        }
      },
      // 下载模板
      downloadTpl() {
        this.$utils.download('/guidance/jpContent/import/template')
      },
      exportExcel() {
        this.$utils.download('/guidance/jpContent/export')
      },
      resetSearch() {
        this.$refs.searchForm.resetFields()
        this.selectJpEvidenceName = ''
        this.refreshList()
      },
      jump(a) {
        var one = '（一）故意伤害行为方面的证据'
        var two = '（二）故意伤害结果方面的证据'
        var three = '（三）故意伤害行为与结果间存在因果关系的证据'
        var four = '（四）行为人方面的证据'
        var five = '（一）盗窃案件的证据收集'
        var six = '（二）掩饰、隐瞒犯罪所得案件的证据收集'
        var seven = '（三）破坏易燃易爆设备案件的证据'
        var eight = '（一）“醉酒驾驶”类危险驾驶的证据收集'
        var nine = '（二）“追逐竞驶”类危险驾驶的证据收集'
        var ten = '（三）“从事校车业务”或“旅客运输”类危险驾驶的证据收集'
        var eleven = '（四）违反危险化学品安全管理规定运输危险化学品类危险驾驶的证据收集'
        if (a === one) {
          this.$router.push('/guidance/one')
        } else if (a === two) {
          this.$router.push('/guidance/two')
        } else if (a === three) {
          this.$router.push('/guidance/three')
        } else if (a === four) {
          this.$router.push('/guidacne/four')
        } else if (a === five) {
          this.$router.push('/guidance/five')
        } else if (a === six) {
          this.$router.push('/guidance/six')
        } else if (a === seven) {
          this.$router.push('/guidance/seven')
        } else if (a === eight) {
          this.$router.push('/guidance/eight')
        } else if (a === nine) {
          this.$router.push('/guidance/nine')
        } else if (a === ten) {
          this.$router.push('/guidance/ten')
        } else if (a === eleven) {
          this.$router.push('/guidance/eleven')
        }
      }
    }
  }
</script>
